package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.entity.EntityReceivingAddress;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

//收货地址编辑界面
public class ReceiveAddressUpdateActivity extends BaseActivity{

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_delete_address)
    TextView tvDeleteAddress;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.et_door)
    EditText etDoor;
    @BindView(R.id.tv_confirm)
    TextView tvConfimr;

    int updateType;//0-新增，1-修改地址
    EntityReceivingAddress address;
    IPresenter presenter;

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_receive_address_update;
    }

    public static Intent createIntent(Context context, EntityReceivingAddress address,int type) {
        Intent intent = new Intent(context, ReceiveAddressUpdateActivity.class);
        intent.putExtra("type",type);
        intent.putExtra("address",address);

        return intent;
    }
    @Override
    public void initParam() {
        updateType=getIntent().getIntExtra("type",0);
        address=(EntityReceivingAddress) getIntent().getSerializableExtra("address");
    }

    @Override
    public void initView() {
        presenter=new PresenterImpl(this);
        updateViewByType();
        tvConfimr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (updateType){
                    case 0://新增
                        String name=etName.getText().toString();
                        String address=etAddress.getText().toString();
                        String phone=etPhone.getText().toString();
                        String doornum=etDoor.getText().toString();

                        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(address) || TextUtils.isEmpty(phone) || TextUtils.isEmpty(doornum) ){
                            ToastUtil.getInstance().shortLength(context,"请将信息补充完整");
                            return;
                        }
                        JSONObject param=new JSONObject();
                        try {
                            param.put("uName",name);
                            param.put("uPhone",phone);

                            param.put("Des",address);
                            param.put("doorNumber",doornum);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        presenter.addReceiveAddress(param.toString());

                        break;
                    case 1://修改
                        String name1=etName.getText().toString();
                        String address1=etAddress.getText().toString();
                        String phone1=etPhone.getText().toString();
                        String doornum1=etDoor.getText().toString();

//                        if (TextUtils.isEmpty(name1) || TextUtils.isEmpty(address1) || TextUtils.isEmpty(phone1) || TextUtils.isEmpty(doornum1) ){
//                            ToastUtil.getInstance().shortLength(context,"请将信息补充完整");
//                            return;
//                        }
                        JSONObject param1=new JSONObject();
                        try {
                            param1.put("recId",ReceiveAddressUpdateActivity.this.address.getID()+"");
                            param1.put("uName",name1);
                            param1.put("uPhone",phone1);

                            param1.put("Des",address1);
                            param1.put("doorNumber",doornum1);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        presenter.updateReceiveAddress(param1.toString());
                        break;
                }
            }
        });


        tvDeleteAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                JSONObject param=new JSONObject();
                try {
                    param.put("receId",address.getID()+"");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                presenter.deleteReceiveAddress(param.toString());
            }
        });
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    public void initOperator() {

    }

    void updateViewByType(){
        switch (updateType){
            case 0://新增
                tvTitle.setText("新增地址");
                tvDeleteAddress.setVisibility(View.GONE);
                break;
            case 1://修改
                tvTitle.setText("修改地址");
                tvDeleteAddress.setVisibility(View.VISIBLE);
                etName.setText(address.getName());
                etPhone.setText(address.getPhone());
                etAddress.setText(address.getDes());
                etDoor.setText(address.getDoorNumber());
                break;
        }

    }

    @Override
    public void onAddReceivingAddressSuccess() {
        super.onAddReceivingAddressSuccess();
        ToastUtil.getInstance().shortLength(context,"添加成功");
        finish();

    }

    @Override
    public void onAddReceivingAddressFail(Exception e) {
        super.onAddReceivingAddressFail(e);
        ToastUtil.getInstance().shortLength(context,e.getMessage());
    }


    @Override
    public void onDeleteReceivingAddressSuccess() {
        super.onDeleteReceivingAddressSuccess();
        ToastUtil.getInstance().shortLength(context,"删除成功");
        finish();
    }

    @Override
    public void onDeleteReceivingAddressFail(Exception e) {
        super.onDeleteReceivingAddressFail(e);
        ToastUtil.getInstance().shortLength(context,e.getMessage());


    }

    @Override
    public void onModifyReceivingAddressSuccess() {
        super.onModifyReceivingAddressSuccess();
        ToastUtil.getInstance().shortLength(context,"修改成功");
        finish();
    }

    @Override
    public void onModifyReceivingAddressFail(Exception e) {
        super.onModifyReceivingAddressFail(e);
        ToastUtil.getInstance().shortLength(context,e.getMessage());

    }
}
